using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Settings
{
    public class AddEditPropertyPage : LayoutsPageBase
    {
        public enum PropertyTypes
        {
            Item, Web
        }

        protected TextBox TxtValue;
        protected TextBox TxtName;
        protected Button BtnDelete;
        protected Button BtnCreate;
        protected Label LblName;

        private PropertyTypes _oPropType;
        private SPWeb _oWeb;
        private SPListItem _oItem;

        private SPWeb GetPropertyWeb
        {
            get
            {
                if (_oWeb == null)
                {
                    if (Request.QueryString["isroot"].ToString() == "false")
                        _oWeb = SPContext.Current.Web;
                    else
                        _oWeb = SPContext.Current.Site.RootWeb;
                }
                return _oWeb;
            }
        }

        private SPListItem GetPropertyItem
        {
            get
            {
                if (_oItem == null)
                {
                    _oItem = SPContext.Current.ListItem;
                }
                return _oItem;
            }
        }

        public override string PageToRedirectOnCancel
        {
            get
            {
                string sUrl = string.Empty;
                switch (_oPropType)
                {
                    case PropertyTypes.Item:
                        sUrl = "ManageItemProperties.aspx";
                        break;
                    case PropertyTypes.Web:
                        sUrl = "ManageWebProperties.aspx";
                        break;
                }
                if (!string.IsNullOrEmpty(this.QS))
                    sUrl += "?" + this.QS;

                return sUrl;
            }
        }

        private string QS
        {
            get
            {
                string sQs = string.Empty;
                if (Request.QueryString.Get("isroot") != null)
                    sQs += "isroot=" + Request.QueryString["isroot"].ToString();
                if (Request.QueryString.Get("List") != null)
                    sQs += ((string.IsNullOrEmpty(sQs)) ? "" : "&") + "List=" + Request.QueryString["List"].ToString();
                if (Request.QueryString.Get("ID") != null)
                    sQs += ((string.IsNullOrEmpty(sQs)) ? "" : "&") + "ID=" + Request.QueryString["ID"].ToString();

                return sQs;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (string.IsNullOrEmpty(Request.QueryString.Get("List")))
                _oPropType = PropertyTypes.Web;
            else
                _oPropType = PropertyTypes.Item;

            string sPropName = string.Empty;
            if(Request.QueryString.Get("Property") != null)
                sPropName = Request.QueryString["Property"].ToString();

            if (string.IsNullOrEmpty(sPropName))
            {
                //Adding a new property
                this.TxtName.Visible = true;
                this.LblName.Visible = false;
                this.BtnDelete.Visible = false;
            }
            else
            {
                string sPropValue = string.Empty;
                sPropValue = GetPropertyWeb.Properties[sPropName].ToString();

                this.LblName.Text = sPropName;

                //Hide delete button if MS internal property : don't want to delete these ones!
                if (sPropName.StartsWith("vti_"))
                {
                    BtnCreate.Visible = false; //Perhaps stop them from being edited too!
                    BtnDelete.Visible = false;
                }

                if (!IsPostBack)
                    this.TxtValue.Text = sPropValue;
            }
        }

        protected void BtnUpdate_Click(object Sender, EventArgs e)
        {
            string sPropName = string.Empty;
            if(Request.QueryString.Get("Property") != null)
                sPropName = Request.QueryString["Property"].ToString();
            else
                sPropName = this.TxtName.Text;

            switch (_oPropType)
            {
                case PropertyTypes.Item:
                    GetPropertyItem.Properties[sPropName] = this.TxtValue.Text;
                    GetPropertyItem.SystemUpdate();
                    break;
                case PropertyTypes.Web:
                    GetPropertyWeb.Properties[sPropName] = this.TxtValue.Text;
                    GetPropertyWeb.Properties.Update();
                    break;
            }
            Response.Redirect(this.PageToRedirectOnCancel);
        }

        protected void BtnDelete_Click(object Sender, EventArgs e)
        {
            string sPropName = string.Empty;
            if (Request.QueryString.Get("Property") != null)
                sPropName = Request.QueryString["Property"].ToString();

            //SS: Call to Remove only removes the key from the dictionary but does not get persisted
            //Setting the value to null does this,
            //GetPropertyWeb.Properties.Remove(sPropName);
            switch (_oPropType)
            {
                case PropertyTypes.Item:
                    GetPropertyItem.Properties[sPropName] = null;
                    GetPropertyItem.SystemUpdate();
                    break;
                case PropertyTypes.Web:
                    GetPropertyWeb.Properties[sPropName] = null;
                    GetPropertyWeb.Properties.Update();
                    break;
            }
            Response.Redirect(this.PageToRedirectOnCancel);
        }
    }
}
